<script setup>
import { reactive, ref } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
import router from '../../../router'
import settings from "@/utils/settings";

const ruleFormRef = ref()
const show=ref(false)
const $route=router
setTimeout(()=>show.value = true,500)
// 校验原密码
const checkPass = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入原密码'))
  }

  if (value.toString().length < 5 || value.toString().length > 10) {
    return callback(new Error('密码长度在5到10位之间'))
  }
  callback()
}
// 校验新密码
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else if (value.toString().length < 5 || value.toString().length > 10) {
    return callback(new Error('密码长度在5到10位之间'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
// 确认密码
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if (value.toString().length < 5 || value.toString().length > 10) {
    return callback(new Error('密码长度在5到10位之间'))
  } else if (!ruleForm.password) {
    return ruleFormRef.value.validateField('password', () => null)
  } else if (value !== ruleForm.password) {
    callback(new Error('两次密码输入不一致'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  password: '',
  checkPass: '',
  oldpass: ''
})

const rules = reactive({
  password: [
    { required: true, message: '输入新密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  checkPass: [
    { required: true, message: '确认新密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ],
  username: [
    { required: true, message: '输入用户账户名', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在5到10位之间', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '输入邮箱验证码', trigger: 'blur' },
    { min: 5, max: 5, message: '验证码为为五位字符串', trigger: 'blur' }
  ]
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      await request.post('/auth/findpassword', ruleForm)
      ElMessage({
        showClose: true,
        message: '修改密码成功！',
        type: 'success'
      })
     $route.go(-1)
    } else {
      return false
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
// 获取验证码
const handleGetEmailCode = async () => {
  if (!ruleForm.username) {
    ElMessage({
      showClose: true,
      message: '账户名不能为空',
      type: 'error'
    })
    return false
  } else {
    await request.get(`/auth/email/${ruleForm.username} `)
    ElMessage({
      showClose: true,
      message: '验证码已经发送到您的邮箱，有效时间为5分钟,请及时填写！',
      type: 'success'
    })
  }
}
</script>
<script>
export default {
  name: 'Forget'
}
</script>
<template>
  <div class="app_login" :style="{backgroundImage: `url(${settings.login_bg_src}`}">


    <el-row justify="center" align="middle" class="login_main">
      <el-col :span="24">
        <div class="login_main_card">
          <el-row>
            <el-col :xs="0" :sm="0" :md="0" :lg="12" :xl="12" class="login_main_col_left">
              <div class="login_main_col_left_container">
                <div style="width: 100%">
                  <el-page-header @back="$route.go(-1)">
                  </el-page-header>
                </div>
               <div style="height: 95%;display: flex;align-items: center;justify-content: center;">
                 <transition name="el-fade-in-linear">
                   <h1 v-if="show">找回密码</h1>
                 </transition>
               </div>

              </div>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="login_main_col_right">

              <div class="login_main_col_right_container">


                <div class="login_main_col_right_container_form">
                  <el-form
                      ref="ruleFormRef"
                      :model="ruleForm"
                      status-icon
                      :rules="rules"
                      label-width="80px"
                      class="demo-ruleForm"
                  >
                    <el-form-item label="账户名&nbsp;&nbsp;&nbsp;" prop="username">
                      <el-input v-model="ruleForm.username" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="新的密码" prop="password">
                      <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                      <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
                    </el-form-item>
                    <el-form-item label="验证码&nbsp;&nbsp;&nbsp;" prop="code">
                      <el-row :gutter="10">
                        <el-col :span="10">
                          <el-input v-model="ruleForm.code" autocomplete="off" />
                        </el-col>
                        <el-col :span="12">
                          <el-button type="warning" @click="handleGetEmailCode">获取邮箱验证码</el-button>
                        </el-col>
                      </el-row>
                    </el-form-item>
                    <div style="display: flex; justify-content: center">
                      <el-button type="primary" @click="submitForm(ruleFormRef)">确认修改 </el-button>
                      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                    </div>
                  </el-form>
                </div>

              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>


  </div>

</template>

<style scoped lang="scss">
.app_login {
  width: 100vw;
  height: 100vh;
  background-repeat: no-repeat;
  background-size: cover;



  .login_main {
    height: 100vh;
    max-width: 50vw;
    min-width: 375px;
    margin: 0 auto;
    .login_main_card {
      background-color: #FFFFFF; /* 设置背景颜色为白色 */
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); /* 设置阴影 */
      border: none; /* 去掉边框 */
      border-radius: 5px; /* 可选：圆角，让卡片更有层次感 */
      overflow: hidden; /* 确保内容不会超出圆角的边界 */
    }


    .login_main_col_left {
      background-image: url("/public/ikon.jpg");
      background-repeat: no-repeat;
      background-size: cover;

      .login_main_col_left_container {

        height: 60vh;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        h1 {
          font-size: 60px;
          font-family: 思源黑体;
          text-align: center;
        }
      }

    }

    .login_main_col_right {

      .login_main_col_right_container {
        height: 60vh;
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        .login_main_col_right_container_form{
          width: 100%;
          padding: 0 30px;
          height: 50%;
        }
      }

    }
  }


}
</style>